package org.hrqing.chess.frame.listener;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hrqing.chess.config.props.SystemConfigProps;
import org.hrqing.chess.frame.CenterBackgroundPanel;
import org.hrqing.chess.frame.ChessLabel;
import org.hrqing.chess.frame.MainFrame;
import org.hrqing.chess.service.LogService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.UUID;

/**
 * @author Hrqing
 */
public class NewMenuItemActionListener implements ActionListener {

    private final MainFrame frame;
    private final CenterBackgroundPanel panel;
    private final SystemConfigProps systemConfigProps;
    private final LogService logService;

    public NewMenuItemActionListener(MainFrame frame, CenterBackgroundPanel panel, SystemConfigProps systemConfigProps, LogService logService) {
        this.frame = frame;
        this.panel = panel;
        this.systemConfigProps = systemConfigProps;
        this.logService = logService;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 主面板（垂直布局，左对齐）
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new  BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10,  10, 10, 10));

        // 单选按钮部分
        JPanel radioPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        radioPanel.add(new  JLabel("游戏难度:"));

        ButtonGroup group = new ButtonGroup();
        JRadioButton option1 = new JRadioButton("简单");
        JRadioButton option2 = new JRadioButton("正常");
        JRadioButton option3 = new JRadioButton("困难");
        group.add(option1);
        group.add(option2);
        group.add(option3);
        radioPanel.add(option1);
        radioPanel.add(option2);
        radioPanel.add(option3);

        switch (systemConfigProps.getDepth().getDepth()) {
            case EASY -> {
                option1.setSelected(true);
            }
            case NORMAL -> {
                option2.setSelected(true);
            }
            case HARD -> {
                option3.setSelected(true);
            }
        }

        mainPanel.add(radioPanel);

        // 输入框部分
        JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        inputPanel.add(new  JLabel("游戏名称:"));
        JTextField textField = new JTextField(15);
        inputPanel.add(textField);
        mainPanel.add(inputPanel);

        // 弹出对话框并获取用户操作结果
        int result = JOptionPane.showConfirmDialog(
                frame,
                mainPanel,
                "新游戏",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.PLAIN_MESSAGE
        );
        if (result == JOptionPane.OK_OPTION) {
            SystemConfigProps.Depth depth = option1.isSelected() ? SystemConfigProps.Depth.EASY : option2.isSelected() ? SystemConfigProps.Depth.NORMAL : SystemConfigProps.Depth.HARD;
            String text = textField.getText();
            if (text != null) {
                String name;
                String gameId = UUID.randomUUID().toString();
                String remark = gameId.split("-")[0];
                if (text.trim().isEmpty()) {
                    name = "new";
                }else {
                    name = text;
                }
                panel.setGameId(gameId);
                panel.setName(name + "_" + remark);
                panel.setDepth(depth);
                ChessLabel.restore(panel);
                LogService.getSteps().clear();
                switch (systemConfigProps.getChess().getColor()) {
                    case RED -> {
                        ChessLabel.selected(panel, ChessLabel.Color.RED);
                        panel.setExecuting(ChessLabel.Color.RED);
                        panel.getExecuteTime().setRed(System.currentTimeMillis());
                    }
                    case BLACK -> {
                        ChessLabel.selected(panel, ChessLabel.Color.BLACK);
                        panel.setExecuting(ChessLabel.Color.BLACK);
                        panel.getExecuteTime().setRed(System.currentTimeMillis());
                    }
                }
                logService.log(panel);
            }
        }
    }
}